package com.blog.controller;

import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


 

import com.blog.dao.UserPoJoMapper;
import com.blog.model.ArticleWithBLOBs;
import com.blog.model.UserPoJo;
import com.blog.service.interfaces.IarticleService;
import com.blog.service.interfaces.IloginService;

@Controller

 
public class LoginController {
	@Autowired
	IloginService loginService ;

	/*
	*已交给 spring security 管理！！！！！！！！！！！！！！！！！！！！
	*/
	/*
	@RequestMapping(value="/login")
	@ResponseBody
	public Map<String, String> loginAction(String username,String password ){
		System.out.println("访问了login");
		Map<String,String> resultMap = new HashMap<String, String>(); 
		//Logger logger=Logger.getLogger(LoginController.class);
		
		if(username==null || password==null){resultMap.put("result", "error");  
        return resultMap; }
		
		//if(loginService.check(user)){
		if(loginService.check2(username,password)){
			//logger.info(username+"登陆成功");
			//需要ajax提交 不是简单的跳转
			resultMap.put("result", "success");  
	        return resultMap;  
		}else{
			//logger.error(username+"密码错误");
			
			resultMap.put("result", "fail");  
	        return resultMap;  
		}
	}*/

	
	
	@RequestMapping(value="/user/register")
	@ResponseBody
	public Map<String, String> registerAction(UserPoJo user){
		//System.out.println("访问了注册");
		Map<String,String> resultMap = new HashMap<String, String>(); 
		
		if(user.getUsername()==null || user.getPassword()==null){resultMap.put("result", "不能为空");  
        return resultMap; }
		
		//if(loginService.check(user)){
		if(loginService.register(user.getUsername(),user.getPassword())==1){
			resultMap.put("result", "注册ok");  return resultMap;
		}else{
			resultMap.put("result", "添加失败，可能是 用户名重复");  return resultMap;
		}
		  
	}
 
	@RequestMapping("/admin")
		public String admin(){
			System.out.println("controller正常 跳转控制台");
		        return "/usercenter/index";  
			
		}
	
 
	@RequestMapping(value="/getjson")
	@ResponseBody
	public Map<String, String> getjson(){
		System.out.println("访问了");
		Map<String,String> resultMap = new HashMap<String, String>(); 
			resultMap.put("result", "得到了json"); 
			resultMap.put("result", "得到了jso2"); 
	        return resultMap;  
	}
	
	@RequestMapping(value="/getjson2")
	@ResponseBody
	public List<TestJson> getjson2(){
		System.out.println("访问了");
			
		List<TestJson> tj=new ArrayList<TestJson>();
			tj.add(new TestJson());tj.add(new TestJson());
	        return tj;  
	}
	class TestJson{
		public String name="name";
		public String pasw="123";
	}
	

}
